package com.powerunion.datacollection.web.struts;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.service.FormCategoryManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.StringUtils;
import com.powerunion.util.log.LogHelper;

public class FormCategoryAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(FormCategoryAction.class);
	private FormCategoryManager fcManager = null;
	
	public FormCategoryAction() {
		super();
		this.fcManager = new FormCategoryManager();
	}
	
	public void finalize() throws Throwable {
		this.fcManager = null;
		super.finalize();
	}
	
	public ActionForward save (ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		String saveStr = request.getParameter("saveStr");

		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("报送模板表单类别信息定义:"+saveStr);
			
			String id = this.fcManager.save(saveStr);
			
	        if(id != null && !id.equals("")) {
	        	response.getWriter().write("{success:true,data:'"+id+"'}");
	        }else{
	        	response.getWriter().write("{failure:true}");
	        }
			
			transaction.commit();
		} catch (Exception e) {
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String[] ids = StringUtils.split(request.getParameter("codes"), ",");
		
		Transaction transaction = null;

		response.setContentType("text/json;charset=utf-8");
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();

			boolean flag = this.fcManager.delete(ids);

			if (flag == true) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}
			transaction.commit();
		} catch (Exception e) {
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
		
	public ActionForward getTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String paraSeq =  request.getParameter("node");	

		JSONArray jsonArray = this.fcManager.getTree(paraSeq);

		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
}
